﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model._Admin;
using HengTong.Model.Db.System;


namespace HengTong.ViewModel._Admin.SysUserFavoriteVms
{
    public partial class SysUserFavoriteListVm : BasePagedListVM<SysUserFavoriteView, SysUserFavoriteSearcher>
    {

        protected override IEnumerable<IGridColumn<SysUserFavoriteView>> InitGridHeader()
        {
            return new List<GridColumn<SysUserFavoriteView>>{
                this.MakeGridHeader(x => x.UserName),
                this.MakeGridHeader(x => x.MenuId),
                this.MakeGridHeader(x => x.MenuName),
                this.MakeGridHeader(x => x.MenuIcon),
                this.MakeGridHeader(x => x.MenuUrl),
                this.MakeGridHeader(x => x.SortOrder),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysUserFavoriteView> GetSearchQuery()
        {
            if(Wtm.LoginUserInfo == null)
            {
                return Enumerable.Empty<SysUserFavoriteView>().AsQueryable().OrderBy(x => x.ID);
            }
            Searcher.UserId = Guid.Parse(Wtm.LoginUserInfo.UserId);
            var query = DC.Set<SysUserFavorite>()
                .CheckEqual(Searcher.UserId, x=>x.UserId)
                .CheckContain(Searcher.MenuId, x=>x.MenuId)
                .CheckContain(Searcher.MenuName, x=>x.MenuName)
                .CheckContain(Searcher.MenuIcon, x=>x.MenuIcon)
                .CheckContain(Searcher.MenuUrl, x=>x.MenuUrl)
                .CheckEqual(Searcher.SortOrder, x=>x.SortOrder)
                .Select(x => new SysUserFavoriteView
                {
				    ID = x.ID,
                    UserName = x.User.Name,
                    MenuId = x.MenuId,
                    MenuName = x.MenuName,
                    MenuIcon = x.MenuIcon,
                    MenuUrl = x.MenuUrl,
                    SortOrder = x.SortOrder,
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysUserFavoriteView : SysUserFavorite{
        [Display(Name = "_Admin.Name")]
        public String UserName { get; set; }

    }
}
